package controller

import (
	v1 "Server/api/v1"
	"Server/internal/service"
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/glog"
)

type loginApi struct {
}

var LoginApi = &loginApi{}

func (api *loginApi) Login(ctx context.Context, req *v1.LoginReq) (res *v1.WebApiRes, err error) {
	user := service.Login().LoginService(req.Uname)
	if res == nil {
		res = &v1.WebApiRes{}
	}
	res.Code = 0
	res.Message = "登录成功"
	res.Success = true
	if user == nil {
		//用户名不对
		glog.Print(ctx, "用户名不正确", req)
		res.Code = -1
		res.Message = "用户名不正确"
		res.Success = false
	}
	if user != nil && user.StaffPass != req.Upass {
		//密码不对
		glog.Print(ctx, "用户名不正确", req)
		res.Code = -1
		res.Message = "密码不正确"
		res.Success = false
	}
	if res.Success {
		mapData := service.RoleMenu().GetRoleMenusByRoleId(user.RoleId)
		v := mapData["MenuIds"]
		result := g.Map{}
		result["user"] = user
		result["menus"] = v
		res.Data = result
	}
	return res, nil
}
